﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Data;
using System.Data.SqlClient;//提供对SqlServer关系数据库访问的Provider
namespace Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //第1步 . 建立数据连接对象
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Server =.\JSON; Database =Demo; Trusted_Connection = True;";
           /*con.ConnectionString = @"Server=.\sqlexpress;Database=Student;User Id=sa;
Password=123456;";//点号代表当前服务器名 */
            //第二步   建立数据操作对象
            string sqlSelect = "select Id,StuNo,StuName,Score from Student";
            con.Open();
            SqlCommand cmd = new SqlCommand(sqlSelect, con);
            con.Close();
            //第三步   数据适配器里显示访问
            SqlDataAdapter dap = new SqlDataAdapter();
            dap.SelectCommand = cmd;
            //第四步   完成数据填充
            DataTable dt = new DataTable("Student");
            dap.Fill(dt);
            //第五步  将数据显示在web页面，通过数据绑定空间Gridview
            GridView1.DataSource = dt;
            GridView1.DataBind();

          //  Student student1 = new Student();
          //  Response.Write("第一个学生的信息:"+"</br>");
          // Response.Write("学号:"+student1 .StuNo+"</br>");

          // Student student2 = new Student("2015002", "张三");
          //Response.Write("第二个学生的信息:"+"</br>");
          // Response.Write("姓名:"+student2.StuName +"</br>");

            //DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");//DateTime.Parse("2007-1-1")
            //List<UserInfo> lstUserInfo = new List<UserInfo>();
            //for (int i = 0; i < 5; i++)
            //{
            //    UserInfo user = new UserInfo();
            //    user.Name ="张"+ i.ToString();
            //    //取随机的日期
            //    Random rnd=new Random(); 
            //    int n=rnd.Next(365);
            //    DateTime dateTime=dateTimeMin.AddDays(n);
            //    user.Birthday = dateTime;
            //    lstUserInfo.Add(user);
            //}   //List泛型集合序列化成JSON的第一种方法
            //var jsonSerializer = new JavaScriptSerializer();
            //string data = jsonSerializer.Serialize(lstUserInfo);   
            //Response.Write(data);
            ////第二种序列化方法
            //string json = JsonConvert.SerializeObject(lstUserInfo);
            //Response.Write("<br/>第二种方法<br/>");
            //Response.Write(json);
        }
    }
}